/*
 * ===========================================================================================
 * = COPYRIGHT
 *          PAX Computer Technology (Shenzhen) Co., Ltd. PROPRIETARY INFORMATION
 *   This software is supplied under the terms of a license agreement or nondisclosure
 *   agreement with PAX Computer Technology (Shenzhen) Co., Ltd. and may not be copied or
 *   disclosed except in accordance with the terms in that agreement.
 *     Copyright (C) 2023 PAX Computer Technology (Shenzhen) Co., Ltd. All rights reserved.
 * ===========================================================================================
 */

import 'package:flutter/material.dart';
import 'package:pos_link/auto_data_source/auto_data_source_common.dart';
import 'package:pos_link/auto_data_source/auto_batch_request_data_source.dart';
import 'package:pos_link/auto_data_source/auto_batch_response_data_source.dart';
import 'package:pos_link/auto_pigeon/poslink_sdk_batch.dart';
import 'package:pos_link/model/response_db.dart';
import 'comm_setting_view_model.dart';

class BatchViewModel extends ChangeNotifier {
  Future startBatch(ResponseDataBase responseDataBase, List<Map> dataSource) async {
    POSLinkBatchApi BatchApi = POSLinkBatchApi();

    /// command
    Map cmdData = dataSource.first;
    switch (cmdData[kValue]) {
      case BatchCommand.SafUploadRequest:
        BatchSafUploadRequest req = BatchReqData.formSafUploadRequest(dataSource);
        BatchSafUploadResponse rsp = await BatchApi.safUpload(req);
        BatchRspData.parseRspBatchSafUploadResponse(rsp, responseDataBase.batchData);
        break;
      case BatchCommand.PurgeBatchRequest:
        BatchPurgeBatchRequest req = BatchReqData.formPurgeBatchRequest(dataSource);
        BatchPurgeBatchResponse rsp = await BatchApi.purgeBatch(req);
        BatchRspData.parseRspBatchPurgeBatchResponse(rsp, responseDataBase.batchData);
        break;
      case BatchCommand.BatchCloseRequest:
        BatchBatchCloseRequest req = BatchReqData.formBatchCloseRequest(dataSource);
        BatchBatchCloseResponse rsp = await BatchApi.batchClose(req);
        BatchRspData.parseRspBatchBatchCloseResponse(rsp, responseDataBase.batchData);
        break;
      case BatchCommand.BatchClearRequest:
        BatchBatchClearRequest req = BatchReqData.formBatchClearRequest(dataSource);
        BatchBatchClearResponse rsp = await BatchApi.batchClear(req);
        BatchRspData.parseRspBatchBatchClearResponse(rsp, responseDataBase.batchData);
        break;
      case BatchCommand.DeleteSafFileRequest:
        BatchDeleteSafFileRequest req = BatchReqData.formDeleteSafFileRequest(dataSource);
        BatchDeleteSafFileResponse rsp = await BatchApi.deleteSafFile(req);
        BatchRspData.parseRspBatchDeleteSafFileResponse(rsp, responseDataBase.batchData);
        break;
      case BatchCommand.ForceBatchCloseRequest:
        BatchForceBatchCloseRequest req = BatchReqData.formForceBatchCloseRequest(dataSource);
        BatchForceBatchCloseResponse rsp = await BatchApi.forceBatchClose(req);
        BatchRspData.parseRspBatchForceBatchCloseResponse(rsp, responseDataBase.batchData);
        break;
      case BatchCommand.DeleteTransactionRequest:
        BatchDeleteTransactionRequest req = BatchReqData.formDeleteTransactionRequest(dataSource);
        BatchDeleteTransactionResponse rsp = await BatchApi.deleteTransaction(req);
        BatchRspData.parseRspBatchDeleteTransactionResponse(rsp, responseDataBase.batchData);
        break;
      default:
        throw Exception('undefined function');
      //break;
    }
  }
}
